﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Arizu.DAL;
using System.Data;
using Arizu.Entity;

namespace Arizu.Repository
{
    public class AirizuPayout : BaseProvider
    {
        #region
        private static volatile AirizuPayout instance;
        private static object lockHelper = new object();

        private AirizuPayout()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AirizuPayout CreateInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new AirizuPayout();
                    }
                }
            }
            return instance;
        }
        #endregion

        /// <summary>
        /// 插入Message信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public long InsertPayoutInfo(PayoutInfoEntity payoutEntity)
        {
            string sql = "INSERT INTO Messages(Type,AccountID,BankAccountSite,BankAccountNum,BankAccountName, UserId) VALUES (@Type,@AccountID,@BankAccountSite,@BankAccountNum,@BankAccountName, @UserId)";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@Type", payoutEntity.Type);
            SqlParameter parm_2 = new SqlParameter("@AccountID", payoutEntity.AccountID);
            SqlParameter parm_3 = new SqlParameter("@BankAccountSite", payoutEntity.BankAccountSite);
            SqlParameter parm_4 = new SqlParameter("@BankAccountNum", payoutEntity.BankAccountNum);
            SqlParameter parm_5 = new SqlParameter("@BankAccountName", payoutEntity.BankAccountName);
            SqlParameter parm_6 = new SqlParameter("@UserId", payoutEntity.UserId);

            SqlParameter[] parameters = { parm_1, parm_2, parm_3, parm_4, parm_5, parm_6 };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

                payoutEntity.ID = long.Parse(ds.Tables[0].Rows[0][0].ToString());

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            return payoutEntity.ID;
        }

        /// <summary>
        /// 获取支付方式信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetPayoutInfo(long userId)
        {
            DataTable dt = new DataTable();
            string sql = "select * from PayoutInfo where UserId=" + userId;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        /// <summary>
        /// 更新支付方式信息
        /// </summary>
        /// <param name="payoutEntity"></param>
        public void UpdatPayoutInfo(PayoutInfoEntity payoutEntity)
        {
            string sql = "Update PayoutInfo Set Type=@Type, AccountID=@AccountID, BankAccountSite=@BankAccountSite, BankAccountNum=@BankAccountNum, BankAccountName=@BankAccountName where UserId=@UserId";

            #region parameters
            SqlParameter parm_1 = new SqlParameter("@Type", payoutEntity.Type);
            SqlParameter parm_2 = new SqlParameter("@AccountID", payoutEntity.AccountID);
            SqlParameter parm_3 = new SqlParameter("@BankAccountSite", payoutEntity.BankAccountSite);
            SqlParameter parm_4 = new SqlParameter("@BankAccountNum", payoutEntity.BankAccountNum);
            SqlParameter parm_5 = new SqlParameter("@BankAccountName", payoutEntity.BankAccountName);
            SqlParameter parm_6 = new SqlParameter("@UserId", payoutEntity.UserId);
            SqlParameter[] parameters = { parm_1, parm_2, parm_3, parm_4, parm_5, parm_6 };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
    }

    /// <summary>
    /// 支付方式
    /// </summary>
    public enum PayoutType : int
    {
        ALIPAY = 0,
        TENPAY = 1,
        PAYPAL = 2,
        BANK = 3
    }
}
